<template>
  <div class="page">
    <div class="main">
      <el-card class="box-card" style="height:800px">
        <div slot="header" class="clearfix">
          <span>
            <navigation />
          </span>
        </div>
        <!-- <div class="list" id="taskScore"> -->
        <b
          style="margin-left:10px;font-size:14px;position: relative;top: -10px;"
          >{{ $route.query.name }}</b
        >
        <el-scrollbar class="page-component__scroll" style="height:100%">
          <div style="height:630px;">
            <el-table
              :header-cell-style="{ background: '#f5f5f5' }"
              :data="tableData"
              style="width: 100%"
              border
              height="620px"
              row-key="id"
              :tree-props="{ children: 'subtasks', hasChildren: 'hasChildren' }"
            >
              <el-table-column
                type="index"
                label="序号"
                align="center"
              ></el-table-column>
              <el-table-column
                prop="targetName"
                label="目标任务名称"
              ></el-table-column>
              <el-table-column
                label="牵头部门"
                prop="departmentName"
              ></el-table-column>
              <el-table-column label="一季度任务量">
                <template slot-scope="scope">
                  {{ embellishDate(scope.row.oneQuarterNumber) }}
                </template>
              </el-table-column>
              <el-table-column label="二季度任务量">
                <template slot-scope="scope">
                  {{ embellishDate(scope.row.twoQuarterNumber) }}
                </template>
              </el-table-column>
              <el-table-column label="三季度任务量">
                <template slot-scope="scope">
                  {{ embellishDate(scope.row.threeQuarterNumber) }}
                </template>
              </el-table-column>
              <el-table-column label="全年任务量">
                <template slot-scope="scope">
                  {{ embellishDate(scope.row.allNumber) }}
                </template>
              </el-table-column>
              <el-table-column fixed="right" width="300" label="操作">
                <template slot-scope="scope">
                  <el-button
                    type="text"
                    :disabled="
                      checkStatus == 2 || checkStatus == 1 ? true : false
                    "
                      v-if="!scope.row.subtasks || scope.row.subtasks.length == 0"
                    @click="addTask('0', scope.row)"
                    >分解</el-button
                  >
                  <el-button type="text" @click="setCheck(scope.row)"
                    v-if="!scope.row.subtasks || scope.row.subtasks.length == 0"
                    >查看</el-button
                  >
                  <el-button
                    type="text"
                    :disabled="
                      checkStatus == 2 || checkStatus == 1 ? true : false
                    "
                    @click="addTask('1', scope.row)"
                    >修改</el-button
                  >
                  <el-button
                    type="text"
                    :disabled="
                      checkStatus == 2 || checkStatus == 1 ? true : false
                    "
                    @click="handleDelete(scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-scrollbar>
        <el-pagination
          background
          style="float:right"
          layout="prev, pager, next"
          :page-size="pageSize"
          :current-page="pageNum"
          :total="total"
          @current-change="pageChange"
        >
        </el-pagination>
        <!-- </div> -->
      </el-card>
    </div>

    <!-- 弹出备注输入框 -->
    <el-dialog title="备注" :visible.sync="dialogVisible" width="30%" center>
      <el-input
        type="textarea"
        :rows="8"
        placeholder="请输入内容"
        v-model="textarea"
      ></el-input>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 弹出框打分 -->

    <el-dialog title="提示" :visible.sync="dialogGrade" width="40%">
      <div>
        <el-switch
          active-color="#13ce66"
          inactive-color="#ff4949"
          v-model="objCurr.type"
          active-text="季度打分"
          inactive-text="月度打分"
        ></el-switch>
        <div style="margin-top:20px;">
          <span style="width:140px;">
            <b style="color:#303133;font-weight: 500;fong-size:12px;">年份</b>
            <el-date-picker
              v-model="objCurr.year"
              type="year"
              size="small"
              placeholder="选择年"
              style="width:134px"
              value-format="yyyy"
            ></el-date-picker>
          </span>
          <span style="width:120px;" class="striping">
            <i>-</i>
            <b
              style="color:#303133;font-weight: 500;fong-size:12px;"
              v-text="objCurr.type ? '季度' : '月份'"
            ></b>
            <el-select
              v-model="objCurr.time"
              size="small"
              placeholder="请选择"
              style="width:100px;"
            >
              <el-option
                v-for="o in a1"
                :key="o"
                :label="o"
                :value="o"
              ></el-option>
            </el-select>
          </span>
        </div>
        <div style="margin-top:20px;">
          <span style="width:140px;">
            <b style="color:#303133;font-weight: 500;fong-size:12px;">分数</b>
          </span>
          <el-input
            size="medium"
            placeholder="请输入分数"
            v-model="objCurr.score"
            style="width:58%;"
          ></el-input>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogGrade = false">取 消</el-button>
        <el-button type="primary" @click="dataGrade()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      dialogVisible: false,
      dialogGrade: false,
      options: [],
      value: "",
      value1: true,
      input: "",
      tableData: [],
      total: 0,
      currentPage1: 5,
      currentPage2: 5,
      currentPage3: 5,
      currentPage4: 4,
      textarea: "",
      objCurr: {
        type: true,
        id: "",
        taskId: "",
        year: "",
        time: "",
        score: ""
      },
      quarterList: [1, 2, 3, 4],
      input2: "",
      taskId: "",
      departmentName: "",
      checkStatus: ""
    };
  },
  mounted() {
    this.taskId = this.$route.query.taskId;
    this.departmentName = this.$route.query.departmentName;
    this.checkStatus = this.$route.query.checkStatus;
    this.getListByPage();
  },
  // 计算属性
  computed: {
    a1() {
      if (this.objCurr.type == true) {
        this.quarterList = [1, 2, 3, 4];
      } else {
        this.quarterList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
      }
      return this.quarterList;
    }
  },
  methods: {
    // 分页
    pageChange(page) {
      this.pageNum = page;
      this.getListByPage();
    },
    // 删除
    handleDelete(val) {
      let _obj = {
        id: val.id
      };
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios
            .post("/distribution/remove", _obj)
            .then(res => {
              if (res.code == 200) {
                this.getListByPage();
                this.$message({
                  message: "数据更新成功",
                  type: "success"
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    embellishDate(text) {
      let _text = "-";
      if (text) {
        _text = text;
      }
      return _text;
    },
    // 新增指标
    addTask(text, val) {
      if (val) {
        if (text == "1") {
          console.log("编辑");
          this.$router.push({
            path: "/RecordList",
            query: {
              type: text,
              parentId: val.taskId,
              departmentName: this.$route.query.departmentName,
              number: val.number,
              upsubtasksId: val.id,
              targetName: val.targetName
            }
          });
          // 列表编辑按钮
        } else {
          // 列表新增按钮
          console.log(val);
          this.$router.push({
            path: "/RecordList",
            query: {
              type: text,
              parentId: this.taskId,
              id: val.id,
              decompose: val.decompose, //是否分解
              targetName: val.targetName
            }
          });
        }
      } else {
        // 顶部新增按钮
        this.$router.push({
          path: "/RecordList",
          query: {
            type: text,
            parentId: this.taskId,
            departmentName: this.$route.query.departmentName,
            number: this.$route.query.number
          }
        });
      }
    },
    setCheck(val) {
      console.log(val);
      this.$router.push({
        path: "/cooperateList",
        query: {
          parentId: this.taskId,
          id: val.id,
          checkStatus: this.checkStatus,
          name: val.targetName
        }
      });
    },
    setGrade(val) {
      console.log(val);
      for (const key in this.objCurr) {
        this.objCurr[key] = "";
      }
      this.objCurr.taskId = val.id;
      this.objCurr.type = true;
      this.dialogGrade = true;
    },
    // 点击列表打分确认按钮
    dataGrade() {
      // let _this = this.objCurr;
      // if (_this.year == "" || _this.time == "" || _this.score == "") {
      //   this.$message.error("请用户填填完输入框");
      //   return;
      // }

      // if (!/(^[1-9]\d*$)/.test(_this.score)) {
      //   this.$message.error("请用户在分数内写入1-100的正整数");
      //   return;
      // }
      // POST /taskScore/add
      this.$axios
        .post(`/taskScore/add`, this.objCurr, 2)
        .then(res => {
          if (res.code == 200) {
            console.log(res);
            this.$message({
              message: "数据更新成功",
              type: "success"
            });
            this.dialogGrade = false;
            this.getListByPage();
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getBack() {
      this.$router.go(-1);
    },
    dataTotal(val) {
      this.pageNum = val;
    },
    // 列表查看按钮
    routerPath(val) {
      // this.$router.push({
      //   path: "/unitboyScore",
      //   query: { subtasksId: val.subtasksId }
      // });
    },
    // 编写备注
    dataRemark() {
      this.dialogVisible = true;
    },
    // 获取数据列表
    getListByPage() {
      let _obj = {
        taskId: this.taskId,
      };
      this.$axios
        .post(`/distribution/getListByPage/${this.pageNum}/${this.pageSize}`, _obj)
        .then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list;
            this.total = res.data.total;
            let _taskId = localStorage.getItem("taskId");
            this.tableData.forEach((item, index) => {
              if (_taskId == "null" || _taskId == "undefined") {
                item.isDisplay = true;
                if (item.subtasks != null || item.subtasks.length != 0) {
                  item.subtasks.forEach((o, i) => {
                    o.isDisplay = true;
                    o.index = `${index + 1}.${i + 1}`;
                  });
                }
                item.index = index + 1;
              } else {
                item.index = index + 1;
                if (this.taskId == _taskId) {
                  item.isDisplay = true;
                  if (item.subtasks != null || item.subtasks.length != 0) {
                    item.subtasks.forEach((o, i) => {
                      o.isDisplay = true;
                      o.index = `${index + 1}.${i + 1}`;
                    });
                  }
                } else {
                  item.isDisplay = false;
                  if (item.subtasks != null || item.subtasks.length != 0) {
                    item.subtasks.forEach((o, i) => {
                      o.isDisplay = true;
                      o.index = `${index + 1}.${i + 1}`;
                    });
                  }
                }
              }
            });

            console.log(this.tableData);
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>
<style scoped>
.page {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.main {
  width: 100%;
  height: 100%;
  /* background: #fff; */
}
.subnav {
  float: left;
  width: 100%;
  height: 60px;
  line-height: 60px;
}
.searchLeft {
  float: left;
  width: 400px;
  /* height: 40px; */
}
.searchLeft > button {
  width: 100px;
  height: 40px;
  background: #409eff;
  color: #fff;
  border: 0px;
  font-size: 16px;
  text-align: center;
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
  position: relative;
  left: -8px;
  top: 1px;
}
.searchRight {
  float: left;
  width: auto;
}
.searchRight > span {
  display: inline-block;
  width: 80px;
  height: auto;
  font-size: 14px;
  text-align: center;
  margin-left: 20px;
}
.el-scrollbar__wrap {
  overflow-x: hidden;
}
.page-component__scroll {
  height: 100%;
}
.list {
  width: 100%;
  /* height: 100%; */
  /* padding-top: 60px; */
  /* padding-bottom: 60px; */
}
.laypage {
  width: 100%;
  height: 60px;

  position: relative;
  top: -60px;
  text-align: right;
}
</style>
<style>
#taskScore .el-scrollbar__wrap {
  overflow: visible;
  overflow-y: scroll;
}
.place {
  float: right;
  position: relative;
  top: -6px;
}
</style>
